/**
 * 用户管理JS
 * @author Assassin
 */
// 添加用户
function executeUserAdd() {
    // 表单验证
    var realName = $.trim($('#realName').val());
    if(shiro.util.isEmpty(realName)) {
        layer.tips('用户姓名不能为空', '#realName');
        return false;
    }
    var telphone = $.trim($('#telphone').val());
    if(shiro.util.isEmpty(telphone)) {
        layer.tips('手机号码不能为空', '#telphone');
        return false;
    }
    var email = $.trim($('#email').val());
    var password = $('#password').val();
    if(shiro.util.isEmpty(password)) {
        layer.tips('登录密码不能为空', '#password');
        return false;
    }
    var rpassword = $('#rpassword').val();
    if(shiro.util.isEmpty(rpassword)) {
        layer.tips('确认密码不能为空', '#rpassword');
        return false;
    }
    if(password != rpassword) {
        layer.tips('两次输入密码不一致', '#rpassword');
        return false;
    }
    var treeObj = $.fn.zTree.getZTreeObj("roleTree");
  	var nodes = treeObj.getCheckedNodes(true);
  	if(nodes.length==0){
  		 layer.msg("请选择用户角色", {icon: 5,time: 1000});
  		 return false;
  	}else{
  		var roleIds = "";
      	for (var i = 0; i < nodes.length; i++) {
      		roleIds += nodes[i].id + ",";
      	}  	
      	$('#roleIds').val(roleIds);
  	}
    // 提交添加
    shiro.ajax({
        url: '/user/executeAddUser',
        data:$('#userAddForm').serialize(),
        success: function(data) {
            if(data.result == 0) {
                layer.msg(data.message, {icon: 1,time: 1000}, function() {
                    window.location.href = shiro.baseUrl + "user/list";
                });
            } else {
                layer.msg(data.message, {icon: 5,time: 1000});
                $('#realName').val('');
            }
        }
    });
    return false;
}

// 修改用户
function executeUserEdit() {
    // 表单验证
    var realName = $.trim($('#realName').val());
    if(shiro.util.isEmpty(realName)) {
        layer.tips('用户姓名不能为空', '#realName');
        return false;
    }
    var telphone = $.trim($('#telphone').val());
    if(shiro.util.isEmpty(telphone)) {
        layer.tips('手机号码不能为空', '#telphone');
        return false;
    }
    var email = $.trim($('#email').val());
    //角色
    var treeObj = $.fn.zTree.getZTreeObj("roleTree");
  	var nodes = treeObj.getCheckedNodes(true);
  	if(nodes.length==0){
  		 layer.msg("请选择用户角色", {icon: 5,time: 1000});
  		 return false;
  	}else{
  		var roleIds = "";
      	for (var i = 0; i < nodes.length; i++) {
      		roleIds += nodes[i].id + ",";
      	}  	
      	$('#roleIds').val(roleIds);
  	}
    // 提交修改
    shiro.ajax({
        url: 'user/executeEditUser',
        data:$('#userEditForm').serialize(),
        success: function(data) {
            if(data.result == 0) {
                layer.msg(data.message, {icon: 1,time: 1000}, function() {
                    window.location.href = shiro.baseUrl + "user/list";
                });
            } else {
                layer.msg(data.message, {icon: 5,time: 1000});
                $('#realName').val('');
            }
        }
    });
    return false;
}

// 删除用户
function deletetUser(userId) {
    layer.msg('确定删除该用户？', {
        time: 0,
        btn: ['确定', '取消'],
        yes: function(index){
            layer.close(index);
            // 提交删除
            shiro.ajax({
                url: 'user/delete',
                data:{"userId": userId},
                success: function(data) {
                    if(data.result == 0) {
                        layer.msg(data.message, {icon: 1,time: 1000}, function() {
                            window.location.href = shiro.baseUrl + "user/list";
                        });
                    } else {
                        layer.msg(data.message, {icon: 5,time: 1000});
                    }
                }
            });
        }
    });
}

// 批量删除用户
function batchDeleteUsers() {
    var userIdArray = shiro.form.getCheckBoxSelectValues('userIdCheckBox');
    if(userIdArray.length == 0) {
        layer.msg('请选择要操作的数据行', {icon: 5,time: 1000});
    } else {
        layer.msg('确定删除选中的用户？', {
            time: 0,
            btn: ['确定', '取消'],
            yes: function(index){
                layer.close(index);
                var userIds = userIdArray.join(',');
                // 提交删除
                shiro.ajax({
                    url: 'user/batchDelete',
                    data:{"userIds": userIds},
                    success: function(data) {
                        if(data.result == 0) {
                            layer.msg(data.message, {icon: 1,time: 1000}, function() {
                                window.location.href = shiro.baseUrl + "user/list";
                            });
                        } else {
                            layer.msg(data.message, {icon: 5,time: 1000});
                        }
                    }
                });
            }
        });
    }
}
//导出
function exportUser(){

    layer.confirm('您确定要导出顾客统计信息到Excel文件？', {title:'系统消息'},function(index){
        $("#wxName_user").val($("#wxName").val());
        $("#realName_user").val($("#realName").val());
        $("#realName_user").val($("#telphone").val());
        $('#exportForm').submit();
        /*shiro.ajax({
            url: 'wxUser/exportUser',
            data:{"wxName": wxName,"realName": realName,"telPhone": telPhone},
            success: function(data) {
               /!* if(data.result == 0) {
                    layer.msg(data.message, {icon: 1,time: 1000}, function() {
                        window.location.href = shiro.baseUrl + "user/list";
                    });
                } else {
                    layer.msg(data.message, {icon: 5,time: 1000});
                }*!/
            }
        });*/
        layer.msg('数据导出中...', {time: 1000});
    });

}